﻿Imports System
Imports System.Collections.Generic
Imports System.Web.Routing
Imports System.Runtime.CompilerServices

Namespace Xhtml
    Public Module ValidationExtensions

        <Extension()> _
        Public Function ValidationMessage(ByVal xHelper As XhtmlHelper, ByVal modelName As String) As XElement
            ' htmlAttributes 
            Return ValidationMessage(xHelper, modelName, DirectCast(Nothing, Object))
        End Function

        <Extension()> _
        Public Function ValidationMessage(ByVal xHelper As XhtmlHelper, ByVal modelName As String, ByVal htmlAttributes As Object) As XElement
            Return ValidationMessage(xHelper, modelName, New RouteValueDictionary(htmlAttributes))
        End Function

        <Extension()> _
        Public Function ValidationMessage(ByVal xHelper As XhtmlHelper, ByVal modelName As String, ByVal message As String) As XElement
            ' htmlAttributes 
            Return ValidationMessage(xHelper, modelName, message, DirectCast(Nothing, Object))
        End Function

        <Extension()> _
        Public Function ValidationMessage(ByVal xHelper As XhtmlHelper, ByVal modelName As String, ByVal message As String, ByVal htmlAttributes As Object) As XElement
            Return ValidationMessage(xHelper, modelName, message, New RouteValueDictionary(htmlAttributes))
        End Function

        <Extension()> _
        Public Function ValidationMessage(ByVal xHelper As XhtmlHelper, ByVal modelName As String, ByVal htmlAttributes As IDictionary(Of String, Object)) As XElement
            ' validationMessage 
            Return ValidationMessage(xHelper, modelName, Nothing, htmlAttributes)
        End Function

        <Extension()> _
        Public Function ValidationMessage(ByVal xHelper As XhtmlHelper, ByVal modelName As String, ByVal message As String, ByVal htmlAttributes As IDictionary(Of String, Object)) As XElement
            If modelName Is Nothing Then
                Throw New ArgumentNullException("modelName")
            End If

            If Not xHelper.ViewData.ModelState.ContainsKey(modelName) Then
                Return Nothing
            End If

            Dim modelState As ModelState = xHelper.ViewData.ModelState(modelName)
            'Dim modelErrors As ModelErrorCollection = If((modelState Is Nothing), Nothing, modelState.Errors)
            'Dim modelError As ModelError = If(((modelErrors Is Nothing) OrElse (modelErrors.Count = 0)), Nothing, modelErrors(0))

            If modelState.Errors.Count = 0 Then
                Return Nothing
            End If

            Dim XMesage = <span><%= message %></span>

            XMesage.SetAttributeValue("class", XhtmlHelper.ValidationMessageCssClassName)
            If htmlAttributes IsNot Nothing Then
                For Each attr In htmlAttributes
                    XMesage.SetAttributeValue(attr.Key, attr.Value)
                Next
            End If

            Return XMesage
        End Function

        <Extension()> _
        Public Function ValidationSummary(ByVal xHelper As XhtmlHelper) As XElement
            ' message 
            Return ValidationSummary(xHelper, Nothing)
        End Function

        <Extension()> _
        Public Function ValidationSummary(ByVal xHelper As XhtmlHelper, ByVal message As String) As XElement
            ' htmlAttributes 
            Return ValidationSummary(xHelper, message, Nothing)
        End Function

        <Extension()> _
        Public Function ValidationSummary(ByVal xHelper As XhtmlHelper, ByVal message As String, ByVal htmlAttributes As Object) As XElement
            Return ValidationSummary(xHelper, message, New RouteValueDictionary(htmlAttributes))
        End Function

        <Extension()> _
        Public Function ValidationSummary(ByVal xHelper As XhtmlHelper, ByVal message As String, ByVal htmlAttributes As IDictionary(Of String, Object)) As XElement
            ' Nothing to do if there aren't any errors
            If xHelper.ViewData.ModelState.IsValid Then
                Return Nothing
            End If

            Dim messageSpan As XElement
            If Not [String].IsNullOrEmpty(message) Then
                messageSpan = <span><%= message %></span>
            Else
                messageSpan = Nothing
            End If

            'Dim htmlSummary As New StringBuilder()
            'Dim unorderedList As New TagBuilder("ul")
            'unorderedList.MergeAttributes(htmlAttributes)
            'unorderedList.MergeAttribute("class", XhtmlHelper.ValidationSummaryCssClassName)

            'For Each modelState As ModelState In xHelper.ViewData.ModelState.Values
            '    For Each modelError As ModelError In modelState.Errors
            '        ' modelState 
            '        Dim errorText As String = GetUserErrorMessageOrDefault(xHelper.ViewContext.HttpContext, modelError, Nothing)
            '        If Not [String].IsNullOrEmpty(errorText) Then
            '            Dim listItem As New TagBuilder("li")
            '            listItem.SetInnerText(errorText)
            '            htmlSummary.AppendLine(listItem.ToString(TagRenderMode.Normal))
            '        End If
            '    Next
            'Next

            'unorderedList.InnerHtml = htmlSummary.ToString()

            Dim errorList As XElement

            If xHelper.ViewData.ModelState.Values.Count > 0 Then
                errorList = <ul>
                                <%= From modelState In xHelper.ViewData.ModelState.Values _
                                    From modelError In modelState.Errors _
                                    Select <li><%= modelError.ErrorMessage %></li> %>
                            </ul>
            Else
                errorList = Nothing
            End If

            Dim result = <div>
                             <%= messageSpan %>
                             <%= errorList %>
                         </div>
            result.SetAttributeValue("class", XhtmlHelper.ValidationSummaryCssClassName)
            If htmlAttributes IsNot Nothing Then
                For Each attr In htmlAttributes
                    result.SetAttributeValue(attr.Key, attr.Value)
                Next
            End If

            Return result

        End Function

    End Module
End Namespace